package cn.wiseda.finance.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class URLCoder {
	private static Logger logger = LoggerFactory.getLogger(URLCoder.class.getName());

	public static String encode(String str) {
		if (str == null) {
			logger.error("不能为Null进行编码");
			return null;
		}
		logger.info("对字符" + str + "串进行编码");
		String result = null;
		try {
			result = URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("字符串" + str + "编码失败", e);
		}
		logger.info("字符串编码成功！");
		return result;
	}

	public static String decode(String str) {
		if (str == null) {
			logger.error("不能为Null进行解码");
			return null;
		}
		logger.info("对字符串" + str + "进行解码");
		String result = null;
		try {
			result = URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("字符串" + str + "解码失败", e);
		}
		logger.info("字符串解码成功！");
		return result;

	}
}
